package com.ftg.youx.demo.workqueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * 生产者
 *
 * @author 布衣
 */
public class ProductChat {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("guest");
        factory.setPassword("guest");
        Connection connection = factory.newConnection();
        //创建频道
        Channel channel = connection.createChannel();
        String queueName = "queue01";
        String name = "张大雷说 : ";
        Scanner scanner = new Scanner(System.in);

        //创建队列
        channel.queueDeclare(queueName, false, false, false, null);

        while(true){
            //发送信息
            String word = scanner.next();
            String message = name + word;
            if(word.equals("退出")){
                break;
            }
            channel.basicPublish("", queueName, null, message.getBytes());
        }
    }
}
